<template>

  <div>
    <div ref="vantaRef" style="width:100%;height:100vh"></div>
    <div class="my_title">
      <el-row class="login-row">
        <el-col :span="12" class="left-column">
          <el-form ref="form" :model="form" :rules="rules" class="login-form">
            <h3 class="title">企康科技后台管理系统</h3>
            <el-form-item prop="userName">
              <el-input v-model="form.userName" placeholder="手机号"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="form.password" type="password" auto-complete="off"
                        placeholder="密码" @keyup.enter.native="onSubmit"></el-input>
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div class="login-reg">

      </div>
    </div>
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 yiyun All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import * as THREE from 'three'
import CLOUDS from 'vanta/src/vanta.clouds'
import { login } from '@/services/user'
export default {
  name: 'LoginIndex',
  mounted () {
    this.vantaEffect = CLOUDS({
      el: this.$refs.vantaRef,
      THREE: THREE
    })
  },
  beforeDestroy () {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  },
  data () {
    return {
      form: {
        userName: '',
        password: ''
      },
      rules: {
        userName: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码长度为 6 到 18 位', trigger: 'blur' }
        ]
      },
      isLoginLoading: false
    }
  },
  methods: {
    // 登录功能
    async onSubmit () {
      try {
        await this.$refs.form.validate()
        this.isLoginLoading = true
        const { data } = await login(this.form)
        this.isLoginLoading = false
        if (data.code === 200) {
          this.$message.success('登录成功')
          // 将用户信息存储到 Vuex 中
          this.$store.commit('setUser', data.data.accessToken)
          // 根据可能存储的 redirect 数据进行跳转设置
          this.$router.push(this.$route.query.redirect || '/')
        } else {
          this.$message.error('登录失败，请联系管理员')
        }
      } catch (err) {
        console.log(err)
        // 设置校验失败后的功能（提示）
        console.log('没有通过校验')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}
.my_title{
  z-index: 1;
  position: fixed; /* 使用fixed使其相对于视口进行定位 */
  top: 50%; /* 将元素的顶部定位在视口的中间 */
  left: 50%; /* 将元素的左侧定位在视口的中间 */
  transform: translate(-50%, -50%); /* 使用负值的50%将元素的中心对齐到视口的中心 */
  .el-form {
    background-color: #fff;
    padding: 20px;
    width: 300px;
    border-radius: 10px;
    .el-button {
      width: 100%;
    }
  }
}
.login {
  height: 100vh;
  display: flex;
  // 设置内部元素垂直水平居中
  justify-content: center;
  align-items: center;
  background-image: url("../../assets/images/6655.jpg");
  background-size: cover;
  .el-form {
    background-color: #fff;
    padding: 20px;
    width: 300px;
    border-radius: 10px;
    .el-button {
      width: 100%;
    }
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  display: flex; // 垂直居中
  align-items: center; // 垂直居中
  justify-content: center; // 水平居中
}
</style>
